﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Data;
using System;

public class AccountHandler : IHander {


    public static event System.Action<Account> OtherLoginHandler;

    public void Register(Dictionary<Protocal, Action<ByteBuffer>> handlers)
    {
        handlers.Add(Protocal.Login_CMD, OnLogin);
        handlers.Add(Protocal.OtherLogin_CMD, OnOtherLogin);
        handlers.Add(Protocal.LogOut_CMD, OnLogOut);

    }
    private void OnLogin(ByteBuffer buffer)
    {
        ReqClient resp = ProtoHelper.Deserialize<ReqClient>(buffer.ReadBytes());
        if (resp.errorCode.code == 1)
        {
            ActorManager.Instance.localUser = resp.localAcot;
            ActorManager.Instance.otherUsers = resp.actors;
            Application.LoadLevel(1);
        }
        else
        {
            Debug.Log(resp.errorCode.error);
        }
    }

    private void OnOtherLogin(ByteBuffer buffer)
    {
        Account resp = ProtoHelper.Deserialize<Account>(buffer.ReadBytes());
        OtherLoginHandler(resp);
    }

    private void OnLogOut(ByteBuffer buffer)
    {
        Account resp = ProtoHelper.Deserialize<Account>(buffer.ReadBytes());
        Debug.Log("玩家:" + resp.nickname + ";退出游戏");
        ActorManager.Instance.RemoveTank(resp.username);
    }
}
